﻿using System;

namespace _1.GenericList
{
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;
        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }


        public void ForEach(Action<T> action)
        {
            Node<T> cur = head;
            while (cur != null)
            {
                action(cur.Data);
                cur = cur.Next;
            }
        }
            
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }else
            {
                tail.Next = n;
                tail = n;
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> intlist = new GenericList<int>();
            Random rd = new Random();
            for(int i = 0; i < 20; ++i)
            {
                int tmp = rd.Next(0,1000);
                intlist.Add(tmp);
            }
            int maxVal = 0, minVal = 100000000, sum = 0;
            Console.WriteLine("链表的数据为：");
            intlist.ForEach((data)=> {
                Console.Write("{0} ",data);
                maxVal = Math.Max(maxVal, data);
                minVal = Math.Min(minVal, data);
                sum += data;
            });
            Console.WriteLine();
            Console.WriteLine("最大值为：{0}",maxVal);
            Console.WriteLine("最小值为：{0}",minVal);
            Console.WriteLine("它们的和为：{0}",sum);



        }
    }
}
